package com.foreveross.chameleon_template;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.RelativeLayout;

import com.foreveross.bsl.util.FileCopeTool;
import com.foreveross.bsl.util.Preferences;
import com.foreveross.bsl.util.ZipUtils;
import com.foreveross.bsl2.R;
import com.foreveross.chameleonsdk.CModule;
import com.foreveross.chameleonsdk.config.URL;
import com.foreveross.push.tool.PadUtils;

public class MainActivity extends Activity {

	private TemplateApplication temphetApplication;
	
	private SharedPreferences  sharePref;
	
	private RelativeLayout layout;
	
//	static final String LoginSuccess = "com.foreveross.loginmodule.loginsuccess";  

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		if (PadUtils.isPad(MainActivity.this)){
			setContentView(R.layout.pad_splash_screen);
		} else {
			setContentView(R.layout.phone_splash_screen);
		}
		temphetApplication = TemplateApplication.class.cast(MainActivity.this.getApplication());
//		goToFirstModule();
//		IntentFilter intentFilter = new IntentFilter(LoginSuccess); 
//		registerReceiver( loginModuleReceiver , intentFilter); 
		
		layout = (RelativeLayout) findViewById(R.id.welcome_loadinglayout);
		
		//第一启动
		sharePref = PreferenceManager.getDefaultSharedPreferences(this);
		Boolean b = Preferences.getFirsttime(sharePref);
		
		String basePath = Environment.getExternalStorageDirectory().getPath() + "/"
				+ URL.APP_PACKAGENAME;
		
		if (b) {
			Preferences.saveFirsttime(false, sharePref);
			UnZipAsynTask unZipAsynTask = new UnZipAsynTask(MainActivity.this);
			unZipAsynTask.execute("www/www.zip" , basePath + "/www/www.zip" ,basePath +"/www");
			Preferences.saveVersionCode(getVersionCode(), sharePref);
		} else {
			int versionCode = Preferences.getVersionCode(sharePref);
			if (versionCode < getVersionCode()){
				Preferences.saveVersionCode(getVersionCode(), sharePref);
				UnZipAsynTask unZipAsynTask = new UnZipAsynTask(MainActivity.this);
				unZipAsynTask.execute("www/www.zip" , basePath + "/www/www.zip" ,basePath +"/www");
			} else {
				init();
			}
			
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
//		unregisterReceiver(loginModuleReceiver);
	}
	public void goToFirstModule(){
		HashMap<String, CModule> modules = temphetApplication.getCApplication().getModules();
		for (CModule module : modules.values()) {
			String activity = module.getActivity();
			if(module.isFirstStart()){
				Intent intent = new Intent();
				intent.setClassName(this, activity);
				this.startActivity(intent);
				finish();
			}
		}
	}
	
	
	/*
	 * 初始化方法实现
	 */
	private void init() {
		// 启动加载页面方式：一
		final long currentStart = System.currentTimeMillis();
		new Thread(new Runnable() {

			public void run() {
				// 休眠标识
				boolean flag = false;
				while (!flag) {
					// 如果休眠时间小于两秒，继续休眠
					if (System.currentTimeMillis() - currentStart > 1000) {
						flag = true;
						goToFirstModule();
					}
				}
			}
		}).start();
	}
	
//	private BroadcastReceiver loginModuleReceiver = new BroadcastReceiver() { 
//	       @Override 
//
//	       public void onReceive(Context context, Intent intent) { 
//	    	   Activity activity = temphetApplication.getCApplication().getActivity();
//	    	   activity.finish();
//	       }
//	    }; 
	
	class UnZipAsynTask extends AsyncTask<String, Integer, Boolean> {

		protected Context context;
		
		public UnZipAsynTask(Context context) {
			this.context = context;
		}

		@Override
		protected void onPreExecute() {
			layout.setVisibility(View.VISIBLE);
			System.out.println(System.currentTimeMillis() + "");
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			if (result){
				System.out.println(System.currentTimeMillis() + "");
				layout.setVisibility(View.GONE);
				Preferences.saveFirsttime(false, sharePref);
				goToFirstModule();;
			}
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			boolean result = false;
			try{
				FileCopeTool tool = new FileCopeTool(context);
				boolean copySuccess = tool.CopyAssetsFile(params[0], params[1]);
				if(copySuccess){
					if (ZipUtils.unZipFile(params[1], params[2])){
//						FileUtil.deleteFile(params[1]);
						result = true;
					} else {
						result = false;
					}
				} else {
					result = false;
				}
			}catch(Exception e){
				result = true;
				e.printStackTrace();
			}
			return result;
		}

	}
	
	public int getVersionCode() {
		PackageManager pm = MainActivity.this.getPackageManager();// context为当前Activity上下文
		PackageInfo pi;
		try {
			pi = pm.getPackageInfo(MainActivity.this.getPackageName(), 0);
			return pi.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
